import type { EventBusListener,EventBusIdentifier } from "./types";


const events = new Map<EventBusIdentifier, Set<EventBusListener<any>>>();

function useEventBus<T = unknown,P = any>(key:EventBusIdentifier) {

  function on(listener: EventBusListener<T,P>) {
    const listeners = events.get(key) ?? new Set();
    listeners.add(listener);
    events.set(key, listeners);
  }

  function emit(event?: T, payload?: P) {
    console.log("验证不传递event的时候event是什么", event);
    events.get(key)?.forEach((f) => f(event, payload));
  }

  return { on, emit };
}

export { useEventBus };
